package ciyuan.Player.Skill.Passive;

import ciyuan.Item.ItemRegisterManager;
import ciyuan.Player.Skill.MySkill;
import ciyuan.Player.Skill.SkillUtils;
import ciyuan.Tasks.PlayerTaskManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.CrossbowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.Hand;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class ChargeAndFill extends MySkill {
    @Override
    public void keyDown(PlayerEntity playerEntity) {

    }

    private static final String ChargeTime = ",ChargeAndFillTime";

    static {
        PlayerTaskManager.register(new PlayerTaskManager.PlayerTask() {
            @Override
            public void run(PlayerEntity playerEntity) {
                ItemStack itemStack = playerEntity.getItemInHand(Hand.MAIN_HAND);
                if (!itemStack.isEmpty()) {
                    if (itemStack.getItem() instanceof CrossbowItem) {
                        SkillUtils.setSaveData(playerEntity, ChargeTime, SkillUtils.getSaveData(playerEntity, ChargeTime) + 1);
                        if (SkillUtils.getSaveData(playerEntity, ChargeTime) > 60) {
                            CompoundNBT compoundnbt = itemStack.getOrCreateTag();
                            if (compoundnbt.contains("Charged")) {
                                boolean isCharged = compoundnbt.getBoolean("Charged");
                                if (!isCharged) {
                                    setCharged(itemStack, true);
                                    addChargedProjectile(itemStack, new ItemStack(ItemRegisterManager.jian1.get()));
                                    SkillUtils.setSaveData(playerEntity, ChargeTime, 0);
                                }
                            } else {
                                setCharged(itemStack, true);
                                addChargedProjectile(itemStack, new ItemStack(ItemRegisterManager.jian1.get()));
                                SkillUtils.setSaveData(playerEntity, ChargeTime, 0);
                            }
                        }
                    }
                }
            }
        });
    }

    private static void setCharged(ItemStack p_220011_0_, boolean p_220011_1_) {
        CompoundNBT compoundnbt = p_220011_0_.getOrCreateTag();
        compoundnbt.putBoolean("Charged", p_220011_1_);
    }

    private static void addChargedProjectile(ItemStack p_220029_0_, ItemStack p_220029_1_) {
        CompoundNBT compoundnbt = p_220029_0_.getOrCreateTag();
        ListNBT listnbt;
        listnbt = new ListNBT();

        CompoundNBT compoundnbt1 = new CompoundNBT();
        p_220029_1_.save(compoundnbt1);
        listnbt.add(compoundnbt1);
        compoundnbt.put("ChargedProjectiles", listnbt);
    }

    @Override
    public String getSkillCastName() {
        return "ChargeAndFill";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.22");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.22");
    }
}
